/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.model.dao;

import hibernate.pojo.Dondathang;
import hibernate.util.HibernateUtil;
import java.sql.Date;
import java.util.Calendar;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author phathuynh
 */
public class DonDatHangDAO {
    
    //<editor-fold defaultstate="collapsed" desc="1. Thêm đơn đặt hàng">
    public static boolean themDonDatHang(Dondathang ddh) {
        boolean flag = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(ddh);
            flag = true;
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
        return flag;
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="2. Lấy ngày hệ thống">
    public static Date LayNgayHeThong()
    {
        Calendar cal = Calendar.getInstance();
        int Nam = cal.get(Calendar.YEAR);
        int Thang = cal.get(Calendar.MONTH) + 1;
        int Ngay = cal.get(Calendar.DATE);  

        String sNam = String.valueOf(Nam);
        String sThang = String.valueOf(Thang);
        String sNgay = String.valueOf(Ngay);
        if(Ngay<10)
        {
            String temp = "0"+sNgay;
            sNgay = temp;
        }
        if(Thang<10)
        {
            String temp = "0"+sThang;
            sThang = temp;
        }
        String fm = String.format("%s-%s-%s", sNam,sThang,sNgay);
        String NgayLapHoaDon = fm;
        Date ngay = Date.valueOf(NgayLapHoaDon);
        return ngay;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="3. Hủy đơn đặt hàng">
    public static boolean huyDonDatHang(Dondathang ddh)
    {
        boolean flag = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tran = null;
        try
        {
            tran = session.beginTransaction();
            session.update(ddh);
            flag = true;
            tran.commit();
        }catch(HibernateException ex)
        {
            System.out.println(ex);
            tran.rollback();
            session.close();
        }
        finally
        {
            session.close();
        }
        return flag;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="4. Lấy đơn đặt hàng by ID">
    
     public static Dondathang layDonDatHang(int donDatHangID)
     {
         Dondathang donDatHang = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
         try
         {
              donDatHang = (Dondathang) session.get(Dondathang.class, donDatHangID);
              //session.close();
         }catch(HibernateException ex)
         {
             System.err.print(ex);
         } finally
         {
             session.close();
         }
         return donDatHang;
     }
    //</editor-fold>
     
     
}
